let app = getApp();
Page({
  data: {
    imgUrl: getApp().globalData.imgUrl,
    ossImg: `${app.globalData.imgUrl}/applet_web_images/new_image`,
    relet_order_no: '', // 账单编号
    billInfo: {}, // 账单信息
    currentRow: {} // 当前选中数据 
  },
  onLoad(options) {
    const { relet_order_no, platform } = options
    this.setData({
      relet_order_no
    })
    my.setNavigationBar({
      frontColor: '#000000',
      backgroundColor: '#f6f6f6'
  })
  let app = getApp();
  app.setShareChannel(platform)
  
  },
  onReady() {
    this.initOrderList()
  },
  initOrderList() {
    const app = getApp()
    const { relet_order_no } = this.data
    my.request({
      url: `${app.globalData.testUrl}/api/relet/getReletOrderBill`,
      method: 'POST',
      data: {
        zm_user_id: app.globalData.user_id, // app.globalData.user_id
        relet_order_no
      },
      success: ({ data: {data, code, msg: content}}) => {
        if(code !== 1) {
          my.showToast({
            type: 'none',
            content,
            duration: 3000
          })
          return
        }
        const repaymentStatus = {
          0: '#999999', // 待还款
          1: '#1777FF', // 已还款
          2: '#F43139', // 逾期中
          3: '#07C700' // 已退款
        }
        data.billInfo = data.relet_bill_list.map(item => {
          item.isSelectData = false // 默认全部不选中
          item.color = repaymentStatus[item.bill_status]
          return item
        })
        
        this.setData({
          billInfo: data
        })
      }
    })
  },

  // 还款
  repayment() {
    let app = getApp()
    const { 
      relet_order_no,
      currentRow: { is_able_payemnt_bill, bill_no } } = this.data
    if(!is_able_payemnt_bill) {
      my.showToast({
        type: 'none',
        content: '无法还款该期账单，请选择首期或其它期账单！',
        duration: 3000
      })
      return
    }
    my.showLoading({
      content: '加载中...',
      delay: 0,
    });
    my.request({
      url: `${app.globalData.testUrl}/api/relet/userRepaymentReletBill`,
      method: 'POST',
      data: {
        relet_order_no,
        zm_user_id: app.globalData.user_id, // app.globalData.user_id
        bill_no
      },
      success: ({data: {data: tradeNO, code, msg: content}}) => {
        if(code !== 1) {
          my.showToast({
            type: 'none',
            content,
            duration: 3000
          })
          my.hideLoading();
          return
        }
        my.tradePay({
          tradeNO,
          success: (res) => {
            const { resultCode: code  } = res
            if(code === '9000') {
              my.showToast({
                type: 'none',
                content: '还款成功',
                duration: 1000,
                success: () => {
                  my.navigateTo({
                    url: '/page/mine/success/success?typeindex=4',
                  });
                }
              })
            } else {
              my.showToast({
                type: 'none',
                content: '还款失败',
                duration: 1000
              });
            }
          },
          fail: () => {
            my.showToast({
              type: 'none',
              content: '还款失败',
              duration: 1000
            })
          },
          complete: () => {
            my.hideLoading();
          }
        })
      }
    })
  },
  // 逾期
  handleOverdue({target: {dataset: { payment_month_rent, overdue_amount }}}) {
    my.alert({
      content:`原租金￥${payment_month_rent} + 逾期金额￥${overdue_amount}`,
      duration: 3000,
      buttonText: '我知道了'
    });
  },
  // 选择账单
  selectData(state, { target: {dataset:{ row, index }}}) {
    this.data.billInfo.relet_bill_list.forEach(item => item.isSelectData = false)
    this.setData({
      ['billInfo.relet_bill_list']: this.data.billInfo.relet_bill_list,
      [`billInfo.relet_bill_list[${index}].isSelectData`]: true,
      currentRow: row
    })
  }
});
